
<!DOCTYPE html>
<html lang="">


<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content>
  

  
    <meta name="description" content="Django中间件">
  
  
  
  <link rel="icon" type="image/x-icon" href="/images/footer-logo.png">
  
  <title>Django中间件 [ 51AIOps 专注于运维自动化  微信： kaipython ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="//cdn.bootcss.com/pure/1.0.0/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
</head>


<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    
    <span class="title" style="text-transform:none">51AIOps 专注于运维自动化  微信： kaipython</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            
              <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
            
          
      
  </ul>
   
</nav>

  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner" style='margin-left:-68px!important'>
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        Django中间件
      </h1>
      <span>
        
        <time class="time" datetime="2019-12-07T16:00:00.000Z">
        2019-12-08
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
    </header>

    <div class="post-content">
      <h3 id="Django-中间件"><a href="#Django-中间件" class="headerlink" title="Django 中间件"></a>Django 中间件</h3><p>Django 中间件是修改 Django request 或者 response 对象的钩子，可以理解为是介于 HttpRequest 与 HttpResponse 处理之间的一道处理过程。</p>
<p>浏览器从请求到响应的过程中，Django 需要通过很多中间件来处理，可以看如下图所示：</p>
<p><img src="https://www.runoob.com/wp-content/uploads/2020/05/1_t9TAX89Y3rZUXth2Le07Xg.png" alt="img"></p>
<p>Django 中间件作用：</p>
<ul>
<li>修改请求，即传送到 view 中的 HttpRequest 对象。</li>
<li>修改响应，即 view 返回的 HttpResponse 对象。</li>
</ul>
<p>中间件组件配置在 settings.py 文件的 MIDDLEWARE 选项列表中。</p>
<p>配置中的每个字符串选项都是一个类，也就是一个中间件。</p>
<p>Django 默认的中间件配置：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">MIDDLEWARE = [</span><br><span class="line">    <span class="string">'django.middleware.security.SecurityMiddleware'</span>,</span><br><span class="line">    <span class="string">'django.contrib.sessions.middleware.SessionMiddleware'</span>,</span><br><span class="line">    <span class="string">'django.middleware.common.CommonMiddleware'</span>,</span><br><span class="line">    <span class="string">'django.middleware.csrf.CsrfViewMiddleware'</span>,</span><br><span class="line">    <span class="string">'django.contrib.auth.middleware.AuthenticationMiddleware'</span>,</span><br><span class="line">    <span class="string">'django.contrib.messages.middleware.MessageMiddleware'</span>,</span><br><span class="line">    <span class="string">'django.middleware.clickjacking.XFrameOptionsMiddleware'</span>,</span><br><span class="line">]</span><br></pre></td></tr></table></figure>

<h3 id="自定义中间件"><a href="#自定义中间件" class="headerlink" title="自定义中间件"></a>自定义中间件</h3><p>中间件可以定义四个方法，分别是：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">process_request(self,request)</span><br><span class="line">process_view(self, request, view_func, view_args, view_kwargs)</span><br><span class="line">process_exception(self, request, exception)</span><br><span class="line">process_response(self, request, response)</span><br></pre></td></tr></table></figure>

<p>自定义中间的步骤：</p>
<p>在 app 目录下新建一个 py 文件，名字自定义，并在该 py 文件中导入 MiddlewareMixin：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">from django.utils.deprecation import MiddlewareMixin</span><br></pre></td></tr></table></figure>

<p><img src="https://www.runoob.com/wp-content/uploads/2020/05/Django-md_2.png" alt="img"></p>
<p>自定义的中间件类，要继承父类 MiddlewareMixin:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">class MD1(MiddlewareMixin): </span><br><span class="line">    pass</span><br></pre></td></tr></table></figure>

<p>在 settings.py 中的 MIDDLEWARE 里注册自定义的中间件类:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">MIDDLEWARE = [</span><br><span class="line">  <span class="string">'django.middleware.security.SecurityMiddleware'</span>,</span><br><span class="line">  <span class="string">'django.contrib.sessions.middleware.SessionMiddleware'</span>,</span><br><span class="line">  <span class="string">'django.middleware.common.CommonMiddleware'</span>,</span><br><span class="line">  <span class="string">'django.middleware.csrf.CsrfViewMiddleware'</span>,</span><br><span class="line">  <span class="string">'django.contrib.auth.middleware.AuthenticationMiddleware'</span>,</span><br><span class="line">  <span class="string">'django.contrib.messages.middleware.MessageMiddleware'</span>,</span><br><span class="line">  <span class="string">'django.middleware.clickjacking.XFrameOptionsMiddleware'</span>,</span><br><span class="line"></span><br><span class="line">  <span class="string">'app01.middlewares.MD1'</span>,</span><br><span class="line">]</span><br></pre></td></tr></table></figure>

<h3 id="自定义中间件类的方法"><a href="#自定义中间件类的方法" class="headerlink" title="自定义中间件类的方法"></a>自定义中间件类的方法</h3><p>自定义中间件类的方法有：process_request 和 process_response。</p>
<p><img src="https://www.runoob.com/wp-content/uploads/2020/05/Django-md_5.png" alt="img"></p>
<h4 id="process-request-方法"><a href="#process-request-方法" class="headerlink" title="process_request 方法"></a>process_request 方法</h4><p>process_request 方法有一个参数 request，这个 request 和视图函数中的 request 是一样的。</p>
<p>process_request 方法的返回值可以是 None 也可以是 HttpResponse 对象。</p>
<ul>
<li>返回值是 None 的话，按正常流程继续走，交给下一个中间件处理。</li>
<li>返回值是 HttpResponse 对象，Django 将不执行后续视图函数之前执行的方法以及视图函数，直接以该中间件为起点，倒序执行中间件，且执行的是视图函数之后执行的方法。</li>
</ul>
<p>process_request 方法是在视图函数之前执行的。</p>
<p>当配置多个中间件时，会按照 MIDDLEWARE中 的注册顺序，也就是列表的索引值，顺序执行。</p>
<p>不同中间件之间传递的 request 参数都是同一个请求对象。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> django.utils.deprecation <span class="keyword">import</span> MiddlewareMixin</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> django.shortcuts <span class="keyword">import</span> render, HttpResponse</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MD1</span><span class="params">(MiddlewareMixin)</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">process_request</span><span class="params">(self, request)</span>:</span></span><br><span class="line">       print(<span class="string">"md1  process_request 方法。"</span>, id(request)) <span class="comment">#在视图之前执行</span></span><br></pre></td></tr></table></figure>

<h4 id="process-response"><a href="#process-response" class="headerlink" title="process_response"></a>process_response</h4><p>process_response 方法有两个参数，一个是 request，一个是 response，request 是请求对象，response 是视图函数返回的 HttpResponse 对象，该方法必须要有返回值，且必须是response。</p>
<p>process_response 方法是在视图函数之后执行的。</p>
<p>当配置多个中间件时，会按照 MIDDLEWARE 中的注册顺序，也就是列表的索引值，倒序执行</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MD1</span><span class="params">(MiddlewareMixin)</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">process_request</span><span class="params">(self, request)</span>:</span></span><br><span class="line">        print(<span class="string">"md1  process_request 方法。"</span>, id(request)) <span class="comment">#在视图之前执行</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">process_response</span><span class="params">(self,request, response)</span>:</span> :<span class="comment">#基于请求响应</span></span><br><span class="line">        print(<span class="string">"md1  process_response 方法！"</span>, id(request)) <span class="comment">#在视图之后</span></span><br><span class="line">        <span class="keyword">return</span> response</span><br></pre></td></tr></table></figure>

<p>从下图看，正常的情况下按照绿色的路线进行执行,假设<strong>中间件1</strong>有返回值，则按照红色的路线走，直接执行该类下的 process_response 方法返回，后面的其他中间件就不会执行。</p>
<p><img src="https://www.runoob.com/wp-content/uploads/2020/05/md-sssss-1.png" alt="img"></p>
<h4 id="process-view"><a href="#process-view" class="headerlink" title="process_view"></a>process_view</h4><p>process_view 方法格式如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">process_view(request, view_func, view_args, view_kwargs)</span><br></pre></td></tr></table></figure>

<p>process_view 方法有四个参数：</p>
<ul>
<li>request 是 HttpRequest 对象。</li>
<li>view_func 是 Django 即将使用的视图函数。</li>
<li>view_args 是将传递给视图的位置参数的列表。</li>
<li>view_kwargs 是将传递给视图的关键字参数的字典。</li>
</ul>
<p>view_args 和 view_kwargs 都不包含第一个视图参数（request）。</p>
<p>process_view 方法是在视图函数之前，process_request 方法之后执行的。</p>
<p>返回值可以是 None、view_func(request) 或 HttpResponse 对象。</p>
<ul>
<li>返回值是 None 的话，按正常流程继续走，交给下一个中间件处理。</li>
<li>返回值是 HttpResponse 对象，Django 将不执行后续视图函数之前执行的方法以及视图函数，直接以该中间件为起点，倒序执行中间件，且执行的是视图函数之后执行的方法。</li>
<li>c.返回值是 view_func(request)，Django 将不执行后续视图函数之前执行的方法，提前执行视图函数，然后再倒序执行视图函数之后执行的方法。</li>
<li>当最后一个中间件的 process_request 到达路由关系映射之后，返回到第一个中间件 process_view，然后依次往下，到达视图函数。</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MD1</span><span class="params">(MiddlewareMixin)</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">process_request</span><span class="params">(self, request)</span>:</span></span><br><span class="line">        print(<span class="string">"md1  process_request 方法。"</span>, id(request)) <span class="comment">#在视图之前执行</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">process_response</span><span class="params">(self,request, response)</span>:</span> :<span class="comment">#基于请求响应</span></span><br><span class="line">        print(<span class="string">"md1  process_response 方法！"</span>, id(request)) <span class="comment">#在视图之后</span></span><br><span class="line">        <span class="keyword">return</span> response</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">process_view</span><span class="params">(self,request, view_func, view_args, view_kwargs)</span>:</span></span><br><span class="line">        print(<span class="string">"md1  process_view 方法！"</span>) <span class="comment">#在视图之前执行 顺序执行</span></span><br><span class="line">        <span class="comment">#return view_func(request)</span></span><br></pre></td></tr></table></figure>

<p><img src="https://www.runoob.com/wp-content/uploads/2020/05/md-32rdf.png" alt="img"></p>
<h3 id="process-exception"><a href="#process-exception" class="headerlink" title="process_exception"></a>process_exception</h3><p>process_exception 方法如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">process_exception(request, exception)</span><br></pre></td></tr></table></figure>

<p>参数说明：</p>
<ul>
<li>request 是 HttpRequest 对象。</li>
<li>exception 是视图函数异常产生的 Exception 对象。</li>
</ul>
<p>process_exception 方法只有在视图函数中出现异常了才执行，按照 settings 的注册倒序执行。</p>
<p>在视图函数之后，在 process_response 方法之前执行。</p>
<p>process_exception 方法的返回值可以是一个 None 也可以是一个 HttpResponse 对象。</p>
<p>返回值是 None，页面会报 500 状态码错误，视图函数不会执行。</p>
<p>process_exception 方法倒序执行，然后再倒序执行 process_response 方法。</p>
<p>返回值是 HttpResponse 对象，页面不会报错，返回状态码为 200。</p>
<p>视图函数不执行，该中间件后续的 process_exception 方法也不执行，直接从最后一个中间件的 process_response 方法倒序开始执行。</p>
<p><strong>具体例子</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MD1</span><span class="params">(MiddlewareMixin)</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">process_request</span><span class="params">(self, request)</span>:</span></span><br><span class="line">        print(<span class="string">"md1  process_request 方法。"</span>, id(request)) <span class="comment">#在视图之前执行</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">process_response</span><span class="params">(self,request, response)</span>:</span> :<span class="comment">#基于请求响应</span></span><br><span class="line">        print(<span class="string">"md1  process_response 方法！"</span>, id(request)) <span class="comment">#在视图之后</span></span><br><span class="line">        <span class="keyword">return</span> response</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">process_view</span><span class="params">(self,request, view_func, view_args, view_kwargs)</span>:</span></span><br><span class="line">        print(<span class="string">"md1  process_view 方法！"</span>) <span class="comment">#在视图之前执行 顺序执行</span></span><br><span class="line">        <span class="comment">#return view_func(request)</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">process_exception</span><span class="params">(self, request, exception)</span>:</span><span class="comment">#引发错误 才会触发这个方法</span></span><br><span class="line">        print(<span class="string">"md1  process_exception 方法！"</span>) </span><br><span class="line">        <span class="comment"># return HttpResponse(exception) #返回错误信息</span></span><br></pre></td></tr></table></figure>


    </div>

  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#Django-中间件"><span class="toc-text">Django 中间件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#自定义中间件"><span class="toc-text">自定义中间件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#自定义中间件类的方法"><span class="toc-text">自定义中间件类的方法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#process-request-方法"><span class="toc-text">process_request 方法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#process-response"><span class="toc-text">process_response</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#process-view"><span class="toc-text">process_view</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#process-exception"><span class="toc-text">process_exception</span></a></li></ol>
  </div>


  </div>
</div>
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=5sr5du46f27&amp;m=0&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33" async="async"></script>
<div class="copyright">
    <span>本作品采用</span>
    <a href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>


  
    <div class="post-nav" style="margin-left:-168px;">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2019/12/08/Django的分页/" rel="next" title="Django的分页">
          Django的分页
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2019/12/09/Django的auth模块/" rel="prev" title="Django 用户认证（Auth）组件">
            Django 用户认证（Auth）组件
          </a>
          <span>〉</span>
        
      </div>
    </div>
  


	
	<div style="width:109%; margin-left:-144px" id="lv-container" data-id="city" data-uid="MTAyMC80OTg5NS8yNjM4Ng==">
	<script type="text/javascript">
   	   (function(d, s) {
       		var j, e = d.getElementsByTagName(s)[0];

       		if (typeof LivereTower === 'function') { return; }

       		j = d.createElement(s);
       		j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
       		j.async = true;

       		e.parentNode.insertBefore(j, e);
   	   })(document, 'script');
	</script>
	<noscript> 为正常使用来必力评论功能请激活JavaScript</noscript>
        </div>
	



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href target="_blank">GitHub</a> |
        <a class="bottom-item" href>友情链接</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/fooying/hexo-theme-xoxo-plus" target="_blank">Theme xoxo-plus</a> |
        <a class="bottom-item" href="/atom.xml">订阅</a>
    </div>
</footer>

  

<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



  

</body>
</html>
